#include <stdio.h>
#include <stdlib.h>
#include <allegro.h>



//collision entre duex objets
int bounding_box_collision(int sprite1_x, int sprite1_y, int sprite1_w,
                           int sprite1_h, int sprite2_x, int sprite2_y, int sprite2_w,
                           int sprite2_h)
{
    if ((sprite1_x > sprite2_x + sprite2_w - 1) || // is Sprite1 on the right side of Sprite2?
            (sprite1_y > sprite2_y + sprite2_h - 1) || // is b1 under b2?
            (sprite2_x > sprite1_x + sprite1_w - 1) || // is b2 on the right side of b1?
            (sprite2_y > sprite1_y + sprite1_h - 1)) // is b2 under b1?
    {
        // no collision
        return 0;
    }

    // collision
    return 1;
}



int main()
{
    int x=0,y=450,xSerp=500,jump=0,direction=0,i;
    char path[255];
    allegro_init();
    if (install_keyboard() == -1)
    {
        allegro_message("Error! %s", allegro_error);
        return (-1);
    }
    set_color_depth(24);
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0) != 0)
    {
        set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
        allegro_message("Unable to initialize video mode!\n%s\n", allegro_error);
        return (1);
    }
    if(install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL))
    {
        allegro_message("Sound Error: %s", allegro_error);
    }
    install_timer();

    BITMAP*buffer=create_bitmap(SCREEN_W,SCREEN_H);
    BITMAP*bg=load_bitmap("taj_mahal_.bmp",NULL);
    BITMAP*serpent=load_bitmap("serpent left 2.bmp",NULL);
    BITMAP*feu=load_bitmap("ooooooooooooo.bmp",NULL);
    BITMAP*jimmyleft[4],*jimmyright[4];

    for(i=0; i<4; i++)
    {
        sprintf(path,"l%d_converted.bmp",i);
        jimmyleft[i]=load_bitmap(path,NULL);
        sprintf(path,"r%d_converted.bmp",i);
        jimmyright[i]=load_bitmap(path,NULL);
    }

    i=0;
    while(!key[KEY_ESC])
    {
        xSerp--;

        if(key[KEY_RIGHT])
        {
            direction=0;
            x+=6;
            i=(i+1)%4;
        }
        if(key[KEY_LEFT])
        {
            direction=1;
            x-=6;
            i=(i+1)%4;
        }
        if(key[KEY_UP] && !jump)
        {
            jump=1;
        }
        if(jump)
        {
            if(jump==1)
            {
                y-=8;
            }
            if(jump==2)
            {
                y+=8;
            }
            if(y<250)
            {
                jump=2;
            }
            if(y>=450)
            {
                y=450;
                jump=0;
            }
        }

        if((bounding_box_collision(x,y,80,80.0 * ((float)jimmyleft[0]->h/jimmyleft[0]->w),xSerp,500,serpent->w,serpent->h) ||
                bounding_box_collision(x,y,80,80.0 * ((float)jimmyleft[0]->h/jimmyleft[0]->w),SCREEN_W-feu->w,SCREEN_H-100,feu->w/2,feu->h/2)) && !jump)
        {
            clear_bitmap(buffer);
            textprintf_centre_ex(buffer,font,SCREEN_W/2,SCREEN_H/2,makecol(255,255,255),-1,"GAME OVER");
            blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
            while(!key[KEY_ESC])
            {
                rest(1);
            }
            break;
        }
        if(x>SCREEN_W)
        {
            clear_bitmap(buffer);
            textprintf_centre_ex(buffer,font,SCREEN_W/2,SCREEN_H/2,makecol(255,255,255),-1,"GAGNEE!!");
            blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
            while(!key[KEY_ESC])
            {
                rest(1);
            }
            break;
        }

        stretch_blit(bg,buffer,0,0,bg->w,bg->h,0,0,SCREEN_W,SCREEN_H);
        stretch_sprite(buffer,feu,SCREEN_W-feu->w,SCREEN_H-100,feu->w/2,feu->h/2);
        stretch_sprite(buffer,serpent,xSerp,500,serpent->w,serpent->h);//100,100.0*(83.0/157.0));
        if(direction)
        {
            stretch_sprite(buffer,jimmyleft[i],x,y,80,80.0 * ((float)jimmyleft[0]->h/jimmyleft[0]->w));
        }
        else
        {
            stretch_sprite(buffer,jimmyright[i],x,y,80,80.0 * ((float)jimmyleft[0]->h/jimmyleft[0]->w));
        }

        blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
        rest(25);
    }


    destroy_bitmap(bg);
    destroy_bitmap(feu);
    destroy_bitmap(serpent);
    for(i=0;i<4;i++){
        destroy_bitmap(jimmyleft[i]);
        destroy_bitmap(jimmyright[i]);
    }
    return 0;
}
